package com.mdd.product.controller;

import com.mdd.common.aop.CurrentUser;
import com.mdd.common.aop.Log;
import com.mdd.common.aop.NotPower;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validate.IdValidate;
import com.mdd.common.validator.annotation.IDMust;
import com.mdd.product.service.IProductSkuService;
import com.mdd.product.service.IProductSpuService;
import com.mdd.product.validate.ProductSpuCreateValidate;
import com.mdd.product.validate.ProductSpuSearchValidate;
import com.mdd.product.validate.ProductSpuUpdateValidate;
import com.mdd.product.validate.car.SpuSaleValidate;
import com.mdd.product.vo.*;
import com.mdd.system.vo.SystemAuthAdminInformVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/spu")
@Api(tags = "商品管理")
public class ProductSpuController {

    @Resource
    IProductSpuService iProductSpuService;
    @Resource
    IProductSkuService iProductSkuService;


    @NotPower
    @GetMapping("/list")
    @ApiOperation(value="商品列表")
    public AjaxResult<List<ProductSpuListedVo>> list(@CurrentUser SystemAuthAdminInformVo systemAuthAdmin) {
        return AjaxResult.success(iProductSpuService.list(systemAuthAdmin));
    }

    @NotPower
    @GetMapping("/page")
    @ApiOperation(value="商品分页列表")
    public AjaxResult<PageResult<ProductSpuListedVo>> page(@Validated ProductSpuSearchValidate searchValidate, @CurrentUser SystemAuthAdminInformVo systemAuthAdmin) {
        return AjaxResult.success(iProductSpuService.page(searchValidate,systemAuthAdmin));
    }
    @NotPower
    @PostMapping("/createSkuOld")
    @ApiOperation(value="生成商品Sku列表")
    public AjaxResult<List<ProductSkuListedVo>> createSkuOld(@RequestBody List<ProductPropertyDetailVo> list) {
        return AjaxResult.success(iProductSkuService.createSkuOld(list));
    }
    @NotPower
    @PostMapping("/createSku")
    @ApiOperation(value="生成商品Sku列表")
    public AjaxResult<List<ProductSkuListedVo>> createSku(@RequestBody CreateSkulVo createSkulVo) {
        return AjaxResult.success(iProductSkuService.createSku(createSkulVo));
    }
    @NotPower
    @PostMapping("/listSkuBySpuId")
    @ApiOperation(value="生成商品Sku列表")
    public AjaxResult<List<ProductSkuListedVo>> listSkuBySpuId(@RequestBody IdValidate idValidate) {
        return AjaxResult.success(iProductSkuService.list(idValidate.getId()));
    }
    @NotPower
    @GetMapping("/detail")
    @ApiOperation(value="商品详情")
    public AjaxResult<ProductSpuDetailVo> detail(@Validated @IDMust() @RequestParam("id") Long id) {
        return AjaxResult.success(iProductSpuService.detail(id));
    }
    @NotPower
    @Log(title = "商品新增")
    @PostMapping("/add")
    @ApiOperation(value="商品新增")
    public AjaxResult<Object> add(@Validated @RequestBody ProductSpuCreateValidate createValidate, @CurrentUser SystemAuthAdminInformVo systemAuthAdmin) {
        iProductSpuService.add(createValidate,systemAuthAdmin);
        return AjaxResult.success();
    }
    @NotPower
    @Log(title = "商品编辑")
    @PostMapping("/edit")
    @ApiOperation(value="商品编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody ProductSpuUpdateValidate updateValidate) {
        iProductSpuService.edit(updateValidate);
        return AjaxResult.success();
    }
    @NotPower
    @Log(title = "商品删除")
    @PostMapping("/del")
    @ApiOperation(value="商品删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iProductSpuService.del(idValidate.getId());
        return AjaxResult.success();
    }
    @NotPower
    @Log(title = "商品上下架")
    @PostMapping("/onSale")
    @ApiOperation(value="商品上下架")
    public AjaxResult<Object> onSale(@Validated @RequestBody SpuSaleValidate saleValidate) {
        iProductSpuService.onSale(saleValidate);
        return AjaxResult.success();
    }
}
